package com.jerei.store.service.impl;

import com.jerei.store.entity.Address;
import com.jerei.store.entity.District;
import com.jerei.store.mapper.AddressMapper;
import com.jerei.store.service.IAddressService;
import com.jerei.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Value("${project.address-max-count}")
    private Integer addressMaxCount;

    @Value("${project.find-parent}")
    private Integer parent;

    @Override
    public void addAddress(Address address, Integer uid, String username) {
        //输出日志
        System.err.println("AddressServiceImpl.addAddress()");
        //根据参数uid查询收货地址数量
        Integer count = addressMapper.countByUid(uid);
        //判断收货地址是否超出上限
        if(count >=addressMaxCount){
            //是：抛出AddressCountLimitExc
            throw new AddressCountLimitException("添加收货地址失败！数量已达上限"+addressMaxCount);
        }

        //判断数量是否为0  得到isDefault的值
        Integer isDefault = count ==0?1:0;
        //补全相关数据：uid is_default  4项日志
        String provinceName=addressMapper.findBycode(Integer.parseInt(address.getProvinceCode()));
        System.err.println("添加地址查回来的省份："+provinceName);
        address.setProvinceName(provinceName);
        String cityName=addressMapper.findBycode(Integer.parseInt(address.getCityCode()));
        System.err.println("添加地址查回来的城市："+cityName);
        address.setCityName(cityName);
        String areaName=addressMapper.findBycode(Integer.parseInt(address.getAreaCode()));
        System.err.println("添加地址查回来的地区："+areaName);
        address.setAreaName(areaName);


        address.setUid(uid);
        address.setIsDefault(isDefault);
        Date now =new Date();
        address.setCreatedUser(username);
        address.setCreatedTime(now);
        address.setModifiedUser(username);
        address.setModifiedTime(now);
        //调用持久层方法实现数据插入。得到受影响行数
        Integer rows=addressMapper.insertAddress(address);
        //判断受影响行数是否不为1
        if(rows!=1){
            //抛出InsertException
            throw new InsertException("添加地址失败！发生未知错误！请联系管理员");
        }
    }

    /**
     * 查询所有的省份
     * @return
     */
    @Override
    public List<District> findAllProvince() {


        List<District> list= addressMapper.findAllProvince(parent);
        if(list.isEmpty()){
            throw new SelectException("查询失败！请重新尝试");
        }
        return list;
    }

    /**
     * 查询所有的城市
     * @return
     */
    @Override
    public List<District> findAllCity(Integer code) {
        if(code==0){
            throw new SelectException("查询失败！请选择省份");
        }
        List<District> list= addressMapper.findAllCity(code);
        if(list.isEmpty()){
            throw new SelectException("查询失败！请重新尝试");
        }
        return list;
    }

    /**
     * 查询所有的地区
     * @return
     */
    @Override
    public List<District> findAllArea(Integer code) {
        if(code==0){
            throw new SelectException("查询失败！请选择城市");
        }
        List<District> list= addressMapper.findAllArea(code);
        if(list.isEmpty()){
            throw new SelectException("查询失败！请重新尝试");
        }
        return list;

    }

    /**
     * 查询该uid下的所有地址
     * @return
     */
    @Override
    public List<Address> findAllByUid(Integer uid) {

        List<Address> list= addressMapper.findAllByUid(uid);

        return list;
    }

    @Override
    public void deleteAddress(Integer aid) {
        System.err.println("执行删除操作");
        System.err.println("获取到的aid="+aid);
        Integer rows=addressMapper.deleteByAid(aid);
        if(rows!=1){
            throw new DeleteException("删除失败！发生未知错误");
        }


    }

    @Override
    @Transactional
    public void setDefault(Integer aid,Integer uid,String username) {
        System.err.println("设置默认，aid="+aid);
        Integer rows;
        List<Address> list= addressMapper.findAllByUid(uid);
        Date date =new Date();
        for(Address a:list){
            if(a.getIsDefault()==1){
                 rows= addressMapper.setDefault(a.getAid(),0,date,username);
                if(rows!=1){
                    throw new UpdateException("1.设置默认失败");
                }
            }
        }
        rows=addressMapper.setDefault(aid,1,date,username);
        if(rows!=1){
            throw new UpdateException("2.设置默认失败");
        }

    }


}
